package com.wocao.controller.uniapp;

import com.wocao.entity.ResponseResult;
import com.wocao.service.ZyOwnerService;
import com.wocao.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("uni-app")
public class UniUploadController {

    @Autowired
    private ZyOwnerService zyOwnerService;

    /**
     * 头像上传
     *
     * @param file file
     * @return return
     * @throws IOException IOException
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file) throws IOException {
        //获取上传的文件名
        String filename = file.getOriginalFilename();
        //调用OssUtils工具类的upload方法，将文件流上传到云存储（OSS）
        OssUtils.upload(file.getInputStream(), filename);
        //获取上传后的文件URL
        String url = OssUtils.getURL(filename);
        return ResponseResult.ok(url);
    }

    //小程序-文件上传
    @PostMapping("/sugUploadImages")
    public ResponseResult<String> sugUploadImages(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OssUtils.upload(file.getInputStream(),filename);
        String url = OssUtils.getURL(filename);
        String substring = url.substring(0, url.indexOf("?"));
        return ResponseResult.ok(substring);
    }
}
